package com.example.myapplication.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.ExeceriseActivity;
import com.example.myapplication.R;
import com.example.myapplication.bean.Excerise;

import java.util.ArrayList;
import java.util.List;

public class ExceriseAdapter extends BaseAdapter implements View.OnClickListener {
    private List<Excerise> exceriseList = new ArrayList<>();
    private Context mContext;
    private Object Intent;

    public ExceriseAdapter(List<Excerise> list,Context ctx){

        mContext = ctx ;
        exceriseList = list;
    }
    @Override
    public int getCount() {
        return exceriseList == null ? 0 : exceriseList.size();
    }

    @Override
    public Object getItem(int position) {
        return exceriseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        List<Object> holder = new ArrayList<>();
        if (convertView ==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.title1,null);
            TextView idTv = convertView.findViewById(R.id.id);
            TextView titleTv = convertView.findViewById(R.id.title);
            holder.add(idTv);
            holder.add(titleTv);
            holder.add(new Integer(position));
            convertView.setTag(holder);
            convertView.setOnClickListener(this);
        }else {
            holder = (List<Object>) convertView.getTag();
        }
        ((TextView)holder.get(0)).setText( String.valueOf(position+1));
        ((TextView)holder.get(1)).setText(exceriseList.get(position).title);
        if (exceriseList.get(position).color == 0){
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_1);
        }else if (exceriseList.get(position).color == 1){
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_2);
        }else if (exceriseList.get(position).color == 2){
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_3);
        }else if (exceriseList.get(position).color == 3){
            ((TextView)holder.get(0)).setBackgroundResource(R.drawable.exercises_bg_4);
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        List<Object> holder = (List<Object>) v.getTag();
        int postion = (Integer)holder.get(2);
        Intent intent = new Intent(mContext, ExeceriseActivity.class);

        intent.putExtra("标题",exceriseList.get(postion).title);
        intent.putExtra("链接",exceriseList.get(postion).url);
        mContext.startActivity(intent);

        Toast.makeText(mContext,(postion+1)+"被点击了",Toast.LENGTH_SHORT).show();

    }

}
